<?
session_start();
$usuario = $_SESSION['S_USUARIO'];
$tipousuario=$_SESSION['S_TIPOUSUARIO'];
$apellidos=$_SESSION['S_APELLIDOS'];
$nombres=$_SESSION['S_NOMBRES'];
include('../funciones_generales.php');
include('../conectar_pg.php');
include('../conectar_my.php');
include('class.ezpdf.php');
$pdf =& new Cezpdf('A4','portrait');
$pdf->selectFont('fonts/Helvetica.afm');
$pdf->ezSetCmMargins(4,1.5,1.5,1.5);
$pdf->ezStartPageNumbers(540,25,6,'','{PAGENUM} de {TOTALPAGENUM}',1);
$pdf->setLineStyle(1);

$anho=$_POST['anho'];
$codcarsec=$_POST['carrera'];
$codcurso=$_POST['codcurso'];
$codasig=$_POST['codasign'];
$ciclo=$_POST['ciclo'];
$convocatoria=$_POST['convocatoria'];
$email=$_POST['email'];
$telef=$_POST['telef'];
$tipoinscriptos=$_POST['tipoinscriptos'];

if ($codcurso=="0") {
	$codcurso_cab="*GENERICO*";
	$codasign_cab="*GENERICO*";
	$asignatura_cab="*GENERICO*";

} else if($codcurso != "0" && $codasig == "0") {
	$codcurso_cab=$codcurso;
	$codasign_cab="*GENERICO*";
	$asignatura_cab="*GENERICO*";

} else if ($codcurso != "0" && $codasig != "0") {
	#buscar asignatura
	$consulta=mysql_query("SELECT descrip FROM codasign where codcarsec='$codcarsec' && codcurso='$codcurso' && codasign='$codasig'");
	$registro=mysql_fetch_row($consulta);
	$codcurso_cab=$codcurso;
	$codasign_cab=$codasig;
	$asignatura_cab=$registro[0];
	
}
if ($email != "" && $telef == "" ) {
	$mensaje_cab="Correo electronico";
} else if ($email == "" && $telef != "" ) {
	$mensaje_cab="Telefono";
} else if ($email != "" && $telef != "" ) {
	$mensaje_cab="Correo electronico y Telefono";
} else if ($email == "" && $telef == "" ) {
	$mensaje_cab="*Ninguno*";
}
#titulo en cada pagina
$all = $pdf->openObject();
$pdf->saveState();
$pdf->addJpegFromFile("escudos/escudo80x74.jpg",50,775,60,54);
$pdf->addText(150,810,10,"<b>TITLE1</b>\n");
$pdf->addText(150,800,10,"<b>TITLE2</b>\n");
if ($tipoinscriptos=="DEF") {
$pdf->addText(150,790,10,"<b><c:uline>Alumnos Inscriptos a Asignaturas - Definitivos   </c:uline></b>");
} elseif($tipoinscriptos=="TMP") {
$pdf->addText(150,790,10,"<b><c:uline>Alumnos Inscriptos  a Asignaturas - Temporales   </c:uline></b>");
} elseif ($tipoinscriptos=="AMB") {
$pdf->addText(150,790,10,"<b><c:uline>Alumnos Inscriptos  a Asignaturas - General   </c:uline></b>");
}
$pdf->addText(150,780,10,"\n");
$pdf->addText(50,760,9,"<b>Periodo:</b> $anho     <b>Carrera:</b> $codcarsec     <b>Curso:</b> $codcurso_cab     <b>CodAsignatura:</b> $codasign_cab\n");
$pdf->addText(50,750,9,"<b>Asignatura:</b> $asignatura_cab \n");
$pdf->addText(50,740,9,"<b>Criterio de contacto a incluir:</b> $mensaje_cab \n");
$pdf->addText(50,25,6,"<b>Obs:</b>'DEF: alumnos matriculados academicamente sin estado de matricula pendiente en caja'");
$pdf->addText(50,18,6,"        'TMP: alumnos matriculados academicamente con estado de matricula pendiente en caja'");

$pdf->restoreState();
$pdf->closeObject();
$pdf->addObject($all,'all');


#VALIDA LA IMPRESION 
if ($tipousuario=="DOC" || $tipousuario=="FUN") {
	if ($codcurso == "0") {
	$sql=mysql_query("SELECT distinct codcurso from usuario_det where usuario='$usuario' and codcarsec='$codcarsec'");
		if (mysql_result($sql,0,"codcurso") != "*") {
			echo "<p align='center'><font face='Tahoma' size='2' color='red'><b>SOLO PUEDE CONSULTAR SUS CURSOS ASIGNADOS!</b></font></p>";
			exit;
		}
	} else if ($codcurso != "0" && $codasig == "0"){
		$sql=mysql_query("SELECT distinct codasign from usuario_det where usuario='$usuario' and codcarsec='$codcarsec' and codcurso='$codcurso'");
		if (mysql_result($sql,0,"codasign") != "*") {
			echo "<p align='center'><font face='Tahoma' size='2' color='red'><b>SOLO PUEDE CONSULTAR SUS ASIGNATURAS ASIGNADAS!</b></font></p>";
			exit;
		}
	}
}
if ($codasig != "0" && $codcurso != "0") {
	$consulta=mysql_query("SELECT descrip FROM codasign where codcarsec='$codcarsec' && codcurso='$codcurso' && codasign='$codasig'");
	$registro=mysql_fetch_row($consulta);
	$asignatura=$registro[0];
}
 

if ($codcurso=="0") {
#echo "IMPRIME POR CARRERA CICLO Y CONVOCATORIA";
#echo $anho."-".$codcarsec."-".$ciclo."-".$convocatoria;
#exit;
	if ($tipoinscriptos=="DEF") {
	$sql="select distinct inscasig.codcarsec,inscasig.anho,inscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno from inscasig, alumno 
	where (inscasig.cedula=alumno.cedula) and (inscasig.anho ='$anho' and inscasig.codcarsec ='$codcarsec' and inscasig.semestre ='$ciclo' and inscasig.convocatoria ='$convocatoria') order by alumno";
	} elseif ($tipoinscriptos=="TMP") {
	$sql= "select distinct buffinscasig.codcarsec,buffinscasig.anho,buffinscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno from buffinscasig, alumno 
	where (buffinscasig.cedula=alumno.cedula) and (buffinscasig.anho ='$anho' and buffinscasig.codcarsec ='$codcarsec' and buffinscasig.semestre ='$ciclo' and buffinscasig.convocatoria ='$convocatoria') order by alumno";
	} elseif ($tipoinscriptos=="AMB") {
	$sql="select distinct inscasig.codcarsec,inscasig.anho,inscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno, 'DEF' as estado from inscasig, alumno 
	where (inscasig.cedula=alumno.cedula) and (inscasig.anho ='$anho' 
	and inscasig.codcarsec ='$codcarsec' and inscasig.semestre ='$ciclo' 
	and inscasig.convocatoria ='$convocatoria')
	union
	select distinct buffinscasig.codcarsec,buffinscasig.anho,buffinscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno,'TMP' from buffinscasig, alumno 
	where (buffinscasig.cedula=alumno.cedula) and (buffinscasig.anho ='$anho' 
	and buffinscasig.codcarsec ='$codcarsec' and buffinscasig.semestre ='$ciclo' 
	and buffinscasig.convocatoria ='$convocatoria') order by alumno";
	}
}elseif($codasig=="0") {
#echo "IMPRIME POR CARRERA Y CURSO";
	if ($tipoinscriptos=="DEF") {
	$sql="select distinct inscasig.codcarsec,inscasig.codcurso,inscasig.anho,inscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno from inscasig, alumno 
	where (inscasig.cedula=alumno.cedula) and (inscasig.anho ='$anho' and inscasig.codcarsec ='$codcarsec' and inscasig.codcurso ='$codcurso' and inscasig.semestre ='$ciclo' and inscasig.convocatoria ='$convocatoria') order by alumno";
	} elseif ($tipoinscriptos=="TMP") {
	$sql="select distinct buffinscasig.codcarsec,buffinscasig.codcurso,buffinscasig.anho,buffinscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno from buffinscasig, alumno 
	where (buffinscasig.cedula=alumno.cedula) and (buffinscasig.anho ='$anho' and buffinscasig.codcarsec ='$codcarsec' and buffinscasig.codcurso ='$codcurso' and buffinscasig.semestre ='$ciclo' and buffinscasig.convocatoria ='$convocatoria') order by alumno";
		
	} elseif ($tipoinscriptos=="AMB") {
	$sql="select distinct inscasig.codcarsec,inscasig.codcurso,inscasig.anho,inscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno,'DEF' as estado from inscasig, alumno 
	where (inscasig.cedula=alumno.cedula) and (inscasig.anho ='$anho' and inscasig.codcarsec ='$codcarsec' 
	and inscasig.codcurso ='$codcurso' and inscasig.semestre ='$ciclo' and inscasig.convocatoria ='$convocatoria')
	union
	select distinct buffinscasig.codcarsec,buffinscasig.codcurso,buffinscasig.anho,buffinscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno, 'TMP' from buffinscasig, alumno 
	where (buffinscasig.cedula=alumno.cedula) and (buffinscasig.anho ='$anho' and buffinscasig.codcarsec ='$codcarsec' 
	and buffinscasig.codcurso ='$codcurso' and buffinscasig.semestre ='$ciclo' 
	and buffinscasig.convocatoria ='$convocatoria') order by alumno";
	}
	
} else {
#echo "IMPRIME POR CARRERA, CURSO Y ASIGNATURA";	
	if ($tipoinscriptos=="DEF") {
	$sql="select distinct inscasig.codcarsec,inscasig.codcurso,inscasig.anho,inscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno from inscasig, alumno 
	where (inscasig.cedula=alumno.cedula) and (inscasig.anho ='$anho' and inscasig.codcarsec='$codcarsec' and inscasig.codcurso='$codcurso' and inscasig.codasign ='$codasig' and inscasig.semestre ='$ciclo' and inscasig.convocatoria ='$convocatoria') order by alumno";
	} elseif ($tipoinscriptos=="TMP") {
	$sql="select distinct buffinscasig.codcarsec,buffinscasig.codcurso,buffinscasig.anho,buffinscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno from buffinscasig, alumno 
	where (buffinscasig.cedula=alumno.cedula) and (buffinscasig.anho ='$anho' and buffinscasig.codcarsec='$codcarsec' and buffinscasig.codcurso='$codcurso' and buffinscasig.codasign ='$codasig' and buffinscasig.semestre ='$ciclo' and buffinscasig.convocatoria ='$convocatoria') order by alumno";
	} elseif ($tipoinscriptos=="AMB") {
	$sql="select distinct inscasig.codcarsec,inscasig.codcurso,inscasig.anho,inscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno, 'DEF' as estado from inscasig, alumno 
	where (inscasig.cedula=alumno.cedula) and (inscasig.anho ='$anho' and inscasig.codcarsec='$codcarsec' 
	and inscasig.codcurso='$codcurso' and inscasig.codasign ='$codasig' and inscasig.semestre ='$ciclo' 
	and inscasig.convocatoria ='$convocatoria') 
	union
	select distinct buffinscasig.codcarsec,buffinscasig.codcurso,buffinscasig.anho,buffinscasig.cedula,
	(alumno.apellido || ', ' || alumno.nombre) as alumno,'TMP' from buffinscasig, alumno 
	where (buffinscasig.cedula=alumno.cedula) and (buffinscasig.anho ='$anho' and buffinscasig.codcarsec='$codcarsec' 
	and buffinscasig.codcurso='$codcurso' and buffinscasig.codasign ='$codasig' and buffinscasig.semestre ='$ciclo' 
	and buffinscasig.convocatoria ='$convocatoria') order by alumno";	
	}
	
}


$resultado = pg_query($db,$sql);

$num_filas = pg_num_rows($resultado);

if ($num_filas<1) {
	echo "<p align='center'><font face='Tahoma' size='2' color='red'><b>NO EXISTEN INSCRIPCIONES PARA LA SELECCION!</b></font></p>";
	exit;
}

$contador=1;

for ($i=0;$i<$num_filas;$i++){
		$codcarsec= pg_fetch_result($resultado, $i, 0);		

		#si es carrera solo, o es curso=0
		if ($codcurso=="0" || $codcurso=="*GENERICO*"){
			$codcurso="*GENERICO*";
			$codasign="*GENERICO*";
			$asignatura="*GENERICO*";
			$anho = pg_fetch_result($resultado, $i, 1);
			$cedula = pg_fetch_result($resultado, $i, 2);
			$alumno = pg_fetch_result($resultado, $i, 3);
			if ($tipoinscriptos=="AMB") {
			$estado = pg_fetch_result($resultado, $i, 4);	
			} elseif ($tipoinscriptos=="DEF") {
			$estado="DEF";
			} elseif ($tipoinscriptos=="TMP") {
			$estado="TMP";	
			}
		#si tiene el curso establecido distinto a cero
		}else{ 
			$codcurso= pg_fetch_result($resultado, $i, 1);

			if ($codasig=="0" || $codasign=="*GENERICO*") {
				$codasign="*GENERICO*";
				$asignatura="*GENERICO*";
				$anho = pg_fetch_result($resultado, $i, 2);
				$cedula = pg_fetch_result($resultado, $i, 3);
				$alumno = pg_fetch_result($resultado, $i, 4);
				if ($tipoinscriptos=="AMB") {
				$estado = pg_fetch_result($resultado, $i, 5);	
				}elseif ($tipoinscriptos=="DEF") {
				$estado="DEF";
				} elseif ($tipoinscriptos=="TMP") {
				$estado="TMP";	
				}

			}else{
				$codasign = $codasig;
				$asignatura=$asignatura;
				$anho = pg_fetch_result($resultado, $i, 2);
				$cedula = pg_fetch_result($resultado, $i, 3);
				$alumno = pg_fetch_result($resultado, $i, 4);
				if ($tipoinscriptos=="AMB") {
				$estado = pg_fetch_result($resultado, $i, 5);	
				}elseif ($tipoinscriptos=="DEF") {
				$estado="DEF";
				} elseif ($tipoinscriptos=="TMP") {
				$estado="TMP";	
				}

			}
		}
		#aqui comienza a buscar los contactos independientemente de que opcion se considera
		if (($email != "") || ($telef != "")) {
			$cedula_alu = trim($cedula);
			$sql = "select * from lime_survey_72352 where 72352X5X89Ced='$cedula_alu'";
			$resultado_buscar = mysql_query($sql);
			$email_alu = mysql_result($resultado_buscar,0,"72352X5X1019");
			$telef_alu = mysql_result($resultado_buscar,0,"72352X5X89Tel");
			if ($email_alu == "") {$email_alu="S/Datos";} 
			if ($telef_alu == "") {$telef_alu="S/Datos";}
		} 
		#aqui imprime de acuerdo a la opcion
		if (($email == "") && ($telef == "")){
				#si no pidio ninguno solo trae los alumnos sin contacto
				$dato[]=array('cuenta'=>$contador,'ced'=>$cedula,'alu'=>$alumno,'estado'=>$estado);
				$titulo=array(
				'cuenta'=>'<b>Id.</b>',
				'ced'=>'<b>Cedula</b>', 
				'alu'=>'<b>Alumno</b>',
				'estado'=>'<b>Estado</b>');
				$opcion=array(
						'shadeCol'=>array(0.9,0.9,0.9),
						'xOrientation'=>'center',
						'width'=>500,
						'fontSize'=>8,
						'protectRows'=>50,
						'cols'=>array('cuenta'=>array('justification'=>'right','width'=>'30'),
						'ced'=>array('justification'=>'center','width'=>'80'),
						'alu'=>array('justification'=>'left'),
						'estado'=>array('justification'=>'center','width'=>'40')));
		} else if (($email != "") && ($telef == "")){
				#si pidio solo email trae los alumnos con contacto
				$dato[]=array('cuenta'=>$contador,'ced'=>$cedula,'alu'=>$alumno,'email'=>$email_alu,'estado'=>$estado);
				$titulo=array(
				'cuenta'=>'<b>Id.</b>',
				'ced'=>'<b>Cedula</b>', 
				'alu'=>'<b>Alumno</b>',
				'email'=>'<b>E-mail</b>',
				'estado'=>'<b>Estado</b>');
				$opcion=array(
						'shadeCol'=>array(0.9,0.9,0.9),
						'xOrientation'=>'center',
						'width'=>500,
						'fontSize'=>8,
						'protectRows'=>50,
						'cols'=>array('cuenta'=>array('justification'=>'right','width'=>'30'),
						'ced'=>array('justification'=>'center','width'=>'80'),
						'alu'=>array('justification'=>'left'),
						'email'=>array('justification'=>'left'),
						'estado'=>array('justification'=>'center','width'=>'40')));
		} else if (($email == "") && ($telef != "")){
				#si pidio solo telef solo trae los alumnos con contacto
				$dato[]=array('cuenta'=>$contador,'ced'=>$cedula,'alu'=>$alumno,'telef'=>$telef_alu,'estado'=>$estado);
				$titulo=array(
				'cuenta'=>'<b>Id.</b>',
				'ced'=>'<b>Cedula</b>', 
				'alu'=>'<b>Alumno</b>',
				'telef'=>'<b>Tel.</b>',
				'estado'=>'<b>Estado</b>');
				$opcion=array(
						'shadeCol'=>array(0.9,0.9,0.9),
						'xOrientation'=>'center',
						'width'=>500,
						'fontSize'=>8,
						'protectRows'=>50,
						'cols'=>array('cuenta'=>array('justification'=>'right','width'=>'30'),
						'ced'=>array('justification'=>'center','width'=>'80'),
						'alu'=>array('justification'=>'left'),
						'telef'=>array('justification'=>'left'),
						'estado'=>array('justification'=>'center','width'=>'40')));
		} else if (($email != "") && ($telef != "")){
				#si pidio ambos trae los alumnos con ambos contacto
				$dato[]=array('cuenta'=>$contador,'ced'=>$cedula,'alu'=>$alumno,'email'=>$email_alu,'telef'=>$telef_alu);
				$titulo=array(
				'cuenta'=>'<b>Id.</b>',
				'ced'=>'<b>Cedula</b>', 
				'alu'=>'<b>Alumno</b>',
				'email'=>'<b>E-mail</b>',
				'telef'=>'<b>Tel.</b>');
				$opcion=array(
						'shadeCol'=>array(0.9,0.9,0.9),
						'xOrientation'=>'center',
						'width'=>500,
						'fontSize'=>7,
						'protectRows'=>50,
						'cols'=>array('cuenta'=>array('justification'=>'right','width'=>'30'),
						'ced'=>array('justification'=>'center','width'=>'60'),
						'alu'=>array('justification'=>'left'),
						'email'=>array('justification'=>'left'),
						'telef'=>array('justification'=>'left')));
		}
	$contador++;	
} //cierra for*/
#titulos genericos	
$txttit = "<b>                               UNIVERSIDAD NACIONAL DE ASUNCION</b>\n";
$txttit.= "<b>                               FACULTAD DE INGENIERIA</b>\n";
$txttit.= "<b>                               <u>Alumnos Inscriptos a Asignaturas    </u></b>\n";
$txttit.= "<b></b>\n";
$txttit.= "<b>Periodo:</b> $anho   <b>Carrera:</b> $codcarsec   <b>Curso:</b> $codcurso   <b>CodAsignatura:</b> $codasign\n";
$txttit.= "<b>Asignatura:</b> $asignatura \n";
$txttit.= "\n";
#$pdf->ezText($txttit, 10);
$tabla=$pdf->ezTable($dato, $titulo, '', $opcion);
$pdf->ezText("<b>Total Alumnos: </b>".$i." (".numletras($i,false,false).")", 8);
$pdf->ezText("\n\n\n\n", 10);
$pdf->ezText("Sistema de Informacion para Docentes - Desarrollado por: DRO/FIUNA/dfvr - 2011", 6);
$pdf->ezText("Usuario: $usuario - $nombres $apellidos", 6);
$pdf->ezText("<b>Fecha:</b> ".date("d/m/Y")." - <b>Hora:</b> ".date("H:i:s"), 6);
$pdf->ezStream();
?>